package com.qubuyer.repository.entitys

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StrikethroughSpan
import com.qubuyer.core.ext.dp
import com.qubuyer.core.ext.format
import java.io.Serializable
import kotlin.math.abs

/**
 * @author Susong
 * @date 创建时间2019/3/21
 * @description 购物车商品实体
 */
class ShopCartGoodEntity : Serializable {
    /**
     * id : 4
     * user_id : 160
     * session_id : i6gkg5s21pes4otmrnd164atb6
     * goods_id : 5
     * goods_sn : TP0000005
     * goods_name : 小米（MI）电视 55英寸 4K 智能WiFi网络平板 智能语音 液晶电视机 标准版 T 4A L55M5-AZ
     * market_price : 3099.00
     * goods_price : 2598.00
     * member_goods_price : 2598.00
     * goods_num : 10
     * item_id : 0
     * spec_key :
     * spec_key_name :
     * bar_code :
     * selected : 1
     * add_time : 1551427589
     * prom_type : 0
     * prom_id : 0
     * sku :
     * combination_group_id : 0
     * cat_id : 354
     * store_count : 100
     * is_on_sale : 1
     * weight : 16300
     * goods : {"goods_id":5,"cat_id":354,"store_count":100,"is_on_sale":1,"prom_type":0,"prom_id":0,"weight":16300,"is_virtual":0}
     */
    var id = 0
    var user_id = 0
    var session_id: String? = null
    var goods_id = 0
    var goods_sn: String? = null
    var goods_name: String? = null
    var market_price: String? = null
    private var nice_market_price: CharSequence? = null
    private var nice_zhekou_price: CharSequence? = null
    private var nice_member_goods_price: CharSequence? = null
    var goods_price: String? = null
    var member_goods_price: String? = null
    var goods_num = 0
    var item_id = 0
    var spec_key: String? = null
    var spec_key_name: String? = null
    var bar_code: String? = null
    var selected = 0
    var add_time = 0
    var prom_type = 0
    var prom_id = 0
    var sku: String? = null
    var combination_group_id = 0
    var cat_id = 0
    var store_count = 0
    var is_on_sale = 0
    var weight = 0
    var goods: HomeGoodEntity? = null

    val niceMarketPrice: CharSequence?
        get() {
            if (nice_market_price == null) {
                val sb = SpannableStringBuilder(market_price)
                sb.setSpan(StrikethroughSpan(), 0, sb.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                nice_market_price = sb
            }
            return nice_market_price
        }

    val niceZheKouPrice: CharSequence?
        get() {
            if (nice_zhekou_price == null) {
                nice_zhekou_price = try {
                    val big = market_price!!.toDouble()
                    val small = member_goods_price!!.toDouble()
                    val offset = abs(big - small)
                    "折让" + offset.format().toString()
                } catch (e: Exception) {
                    "折让0.00"
                }
            }
            return nice_zhekou_price
        }

    val niceMemberPrice: CharSequence?
        get() {
            if (nice_member_goods_price == null) {
                nice_member_goods_price = SpannableStringBuilder("¥$member_goods_price").apply {
                    setSpan(ForegroundColorSpan(Color.parseColor("#999999")), 0, this.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                    setSpan(AbsoluteSizeSpan(11.dp), 0, this.indexOf("¥") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                    setSpan(AbsoluteSizeSpan(14.dp), this.indexOf("¥") + 1, this.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                }
            }
            return nice_member_goods_price
        }
}